<template>
  <Form
    class="xtx-form"
    ref="FormCom"
    :validation-schema="VeeValidateSchema"
    v-slot="{ errors }"
  >
    <div class="user-info">
      <img
        src="http://qzapp.qlogo.cn/qzapp/101941968/57C7969540F9D3532451374AA127EE5B/50"
        alt=""
      />
      <p>
        Hi，{{ data.nickname }} 欢迎来小兔鲜，完成绑定后可以QQ账号一键登录哦~
      </p>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <Field
          class="input"
          name="mobile"
          v-model="form.mobile"
          type="text"
          placeholder="绑定的手机号"
        />
      </div>
      <div class="error">{{ errors.mobile }}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-code"></i>
        <Field
          class="input"
          type="text"
          name="code"
          v-model="form.code"
          placeholder="短信验证码"
        />
        <span class="code" @click="sendCode">{{time===0?'发送验证码':time}}</span>
      </div>
      <div class="error">{{ errors.code }}</div>
    </div>
    <a href="javascript:;" class="submit" @click="sendLogin">立即绑定</a>
  </Form>
</template>

<script>
import VeeValidateSchema from '@/utils/vee-validate-schema'
import { inject, onUnmounted, reactive, ref } from 'vue'
import { Form, Field } from 'vee-validate'
import Message from '@/utils/Message'
import { userQQBindCode, userQQBindLogin } from '@/api/user'
import { useIntervalFn } from '@vueuse/core'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
export default {
  name: 'CallbackBind',
  components: {
    Form, Field
  },
  setup () {
    const data = inject('qqgetUserInfo')
    const FormCom = ref(null)
    const form = reactive({
      code: '',
      mobile: ''
    })
    const store = useStore()
    const router = useRouter()
    const time = ref(0)
    const { pause, resume } = useIntervalFn(() => {
      time.value--
      if (time.value <= 0) {
        pause()
      }
    }, 1000, {
      immediate: false
    })
    const sendCode = async () => {
      const validate = VeeValidateSchema.mobile(form.mobile)
      if (validate === true) {
        try {
          await userQQBindCode(form.mobile)
          time.value = 60
          resume()
        } catch (error) {
          console.dir(error)
          Message({ title: error.response.data.message, type: 'error' })
        }
      } else {
        Message({ title: validate, type: 'error' })
      }
    }
    onUnmounted(() => {
      pause()
    })
    const sendLogin = async () => {
      const res = await FormCom.value.validate()
      try {
        if (res) {
          const { result } = await userQQBindLogin({
            unionId: data.value.openId,
            mobile: form.mobile,
            code: form.code
          })
          store.commit('user/setUser', result)
          Message({ title: '登录成功', type: 'success' })
          console.log(router.currentRoute)
          router.push(router.currentRoute.value.query.url || '/')
        } else {
          Message({ title: '请输入手机号和验证码', type: 'error' })
        }
      } catch (error) {
        Message({ title: error.response.data.message, type: 'error' })
      }
    }
    console.log(data)
    return { data, VeeValidateSchema, sendLogin, form, sendCode, FormCom, time }
  }
}
</script>

<style scoped lang='less'>
.user-info {
  width: 320px;
  height: 70px;
  margin: 0 auto;
  display: flex;
  background: #f2f2f2;
  align-items: center;
  padding: 0 10px;
  margin-bottom: 25px;
  img {
    background: #f2f2f2;
    width: 50px;
    height: 50px;
  }
  p {
    padding-left: 10px;
  }
}
.code {
  position: absolute;
  right: 0;
  top: 0;
  line-height: 50px;
  width: 80px;
  color: #999;
  &:hover {
    cursor: pointer;
  }
}
</style>
